package com.fuint.application.service.useraction;

import com.fuint.application.dao.entities.MtUserAction;
import com.fuint.application.dao.repositories.MtUserActionRepository;
import com.fuint.base.annoation.OperationServiceLog;
import com.fuint.base.dao.pagination.PaginationRequest;
import com.fuint.base.dao.pagination.PaginationResponse;
import com.fuint.exception.BusinessCheckException;
import com.fuint.application.enums.StatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import java.util.*;

/**
 * 会员行为业务接口
 * Created by FSQ
 * Contact wx fsq_better
 * Site https://www.fuint.cn
 */
@Service
public class UserActionServiceImpl implements UserActionService {

    @Autowired
    private MtUserActionRepository userActionRepository;

    /**
     * 分页查询列表
     *
     * @param paginationRequest
     * @return
     */
    @Override
    public PaginationResponse<MtUserAction> queryUserActionListByPagination(PaginationRequest paginationRequest) {
        PaginationResponse<MtUserAction> paginationResponse = userActionRepository.findResultsByPagination(paginationRequest);
        return paginationResponse;
    }

    /**
     * 新增会员行为
     *
     * @param  reqUserAction
     * @throws BusinessCheckException
     */
    @Override
    public boolean addUserAction(MtUserAction reqUserAction) {
        if (reqUserAction.getAction() == null || reqUserAction.getUserId() == null) {
            return false;
        }

        Map<String, Object> params = new HashMap<>();
        params.put("EQ_userId", reqUserAction.getUserId().toString());
        params.put("EQ_action", reqUserAction.getAction());
        if (reqUserAction.getParam() != null) {
            params.put("EQ_param", reqUserAction.getParam());
        }

        Specification<MtUserAction> specification = userActionRepository.buildSpecification(params);
        Sort sort = new Sort(Sort.Direction.ASC, "id");
        List<MtUserAction> dataList = userActionRepository.findAll(specification, sort);

        // 防止重复
        if (dataList.size() == 0) {
            MtUserAction mtUserAction = new MtUserAction();
            mtUserAction.setAction(reqUserAction.getAction());
            mtUserAction.setUserId(reqUserAction.getUserId());
            mtUserAction.setStoreId(reqUserAction.getStoreId());
            mtUserAction.setParam(reqUserAction.getParam());
            mtUserAction.setOperator(reqUserAction.getOperator());
            mtUserAction.setDescription(reqUserAction.getDescription());
            mtUserAction.setStatus(StatusEnum.ENABLED.getKey());
            mtUserAction.setCreateTime(new Date());
            mtUserAction.setUpdateTime(new Date());
            userActionRepository.save(mtUserAction);
        }

        return true;
    }

    /**
     * 根据ID获取信息
     *
     * @param id
     * @throws BusinessCheckException
     */
    @Override
    public MtUserAction getUserActionDetail(Integer id) {
        return userActionRepository.findOne(id);
    }

    /**
     * 根据ID删除
     *
     * @param id
     * @param operator 操作人
     * @throws BusinessCheckException
     */
    @Override
    @OperationServiceLog(description = "删除会员行为")
    public void deleteUserAction(Integer id, String operator) {
        MtUserAction mtUserAction = this.getUserActionDetail(id);
        if (mtUserAction == null) {
            return;
        }

        mtUserAction.setStatus(StatusEnum.DISABLE.getKey());
        mtUserAction.setUpdateTime(new Date());

        userActionRepository.save(mtUserAction);
    }
}
